package com.example.zyfypt516wh.activities;


import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.zyfypt516wh.R;
import com.example.zyfypt516wh.bean.CollectResult;
import com.example.zyfypt516wh.bean.FocusResult;
import com.example.zyfypt516wh.common.Common;
import com.example.zyfypt516wh.iface.CollectionListener;
import com.example.zyfypt516wh.iface.FocusListener;
import com.example.zyfypt516wh.model.CollectionModel;
import com.example.zyfypt516wh.model.FocusModel;

import java.util.List;


/**
 * 加载文章类
 */
public class WebActivity extends AppCompatActivity {
    private WebView webview;
    private TextView tvCollect;
    private TextView tvFollow;

    private SharedPreferences sp;
    String type, id,userid,sessionID,collecStatus,followStatus;
    String focusType="userfocus";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_web);
        sp=this.getSharedPreferences("user",MODE_PRIVATE);
        sessionID=sp.getString("sessionID","");


        type = getIntent().getStringExtra("type");
        id = getIntent().getStringExtra("id");
        userid=getIntent().getStringExtra("userid");

        //初始化数据
        initView();
        //判断是否收藏
        existCollection();

        //判断是否关注
        existFollow();

        //收藏
        tvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (collecStatus.equals("1")){//状态1 尚未收藏
                    toCollection();
                }else if (collecStatus.equals("0")){//状态0 已收藏--》取消收藏
                    unCollection();
                }


            }
        });
        //关注
        tvFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (followStatus.equals("1")){//状态1 未关注
                    toFollow();
                }else if (followStatus.equals("0")){//状态0 已关注--》取消关注
                    unFollow();
                }

            }
        });

    }

    FocusListener existFListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            followStatus = res;
            if (followStatus.equals("1")) {
                tvFollow.setText("关注");
            } else if (followStatus.equals("0")) {
                tvFollow.setText("取消关注");
            }
        }

        @Override
        public void onFail(String msg) {

        }

    };

        /**
     * 判断是否关注
     */
    private void existFollow() {
        FocusModel model=new FocusModel();
        model.exist(focusType,Integer.parseInt(userid),sessionID,existFListener);
    }


    FocusListener focusListener =new FocusListener() {
        @Override
        public void onResponse(List<FocusResult> list) {

        }

        @Override
        public void onResponse(String res) {
            Log.i("TAG", "关注结果onResponse:---》 "+res);
            existFollow();

        }

        @Override
        public void onFail(String msg) {
            System.out.println("判断是否关注请求--->"+msg);
        }
    };

    /**
     * 关注
     */
    private void toFollow() {
        FocusModel model=new FocusModel();
        model.focus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    /**
     * 取消关注
     */
    private void unFollow() {
        FocusModel model=new FocusModel();
        model.unfocus(focusType,Integer.parseInt(userid),sessionID, focusListener);
    }

    CollectionListener existListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            collecStatus=res;
            if (collecStatus.equals("1")){
                tvCollect.setText("收藏");
            }else if (collecStatus.equals("0")){
                tvCollect.setText("取消收藏");
            }
        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };

    //判断是否收藏网络请求
    private void existCollection() {
        CollectionModel model=new CollectionModel();
        model.exist(type,Integer.parseInt(id),sessionID,existListener);
    }

    CollectionListener collectionListener =new CollectionListener() {
        @Override
        public void onResponse(String res) {
            Log.i("TAG", "收藏结果onResponse:---》 "+res);
            existCollection();

        }
        @Override
        public void onResponse(List<CollectResult> list) { }
        @Override
        public void onFail(String msg) {
            System.out.println("判断是否存在请求--->"+msg);
        }
    };
    //收藏网络请求
    private void toCollection() {
        CollectionModel model=new CollectionModel();
        model.collect(type,Integer.parseInt(id),sessionID,collectionListener);
    }

    //取消收藏网络请求
    private void unCollection() {
        CollectionModel model=new CollectionModel();
        model.uncollect(type,Integer.parseInt(id),sessionID,collectionListener);
    }

    private void initView() {
        webview = findViewById(R.id.webview);
        tvCollect = findViewById(R.id.title_collection);
        tvFollow = findViewById(R.id.title_follow);

        WebSettings webSettings = webview.getSettings();
        webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //设置缓存
        webSettings.setJavaScriptEnabled(true);//设置能够解析Javascript
        webSettings.setDomStorageEnabled(true);//设置适应Html5
        webview.loadUrl(Common.BASEURL + "/" + type + ".php/show/index/id/" + id);
    }

    @Override
    protected void onDestroy() {
        if (webview != null)
            webview.destroy();

        super.onDestroy();
    }
}